"""
Case Type   : 关键字
Case Name   : opengauss关键字owner(非保留)，作为目录对象名
Create At   : 2024/9/21
Owner       : lonely-dance
Description :
    1、关键字作为目录对象名不带双引号;
    2、关键字作为目录对象名带双引号;
    3、关键字作为目录对象名带单引号;
    4、关键字作为目录对象名带反引号;
Expect      :
    1、成功;
    2、成功;
    3、失败;
    4、成功;
History     :
"""

import unittest
import os

from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH


logger = Logger()
commonsh = CommonSH('dbuser')
constant = Constant()

class Hostname(unittest.TestCase):


    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')

    # 关键字作为目录对象名不带双引号 - 成功
    def test_owner_1(self):
        SqlMdg = commonsh.execut_db_sql('''create directory owner as '/tmp/';
                                        drop directory owner;''')
        logger.info(SqlMdg)
        self.assertIn(constant.CREATE_DIRECTORY_SUCCESS_MSG, SqlMdg)
        self.assertIn(constant.DROP_DIRECTORY_SUCCESS_MSG, SqlMdg)

    # 关键字作为目录对象名带双引号—成功
    def test_owner_2(self):
        SqlMdg = commonsh.execut_db_sql('''create directory "owner" as '/tmp/';
                                        drop directory "owner";''')
        logger.info(SqlMdg)
        self.assertIn(constant.CREATE_DIRECTORY_SUCCESS_MSG, SqlMdg)
        self.assertIn(constant.DROP_DIRECTORY_SUCCESS_MSG, SqlMdg)

    # 关键字作为目录对象名带单引号 - 合理报错
    def test_owner_3(self):
        SqlMdg = commonsh.execut_db_sql('''drop directory if exists 'owner';''')
        logger.info(SqlMdg)
        self.assertIn(constant.SYNTAX_ERROR_MSG, SqlMdg)

        SqlMdg = commonsh.execut_db_sql(''' create directory 'owner' as '/tmp/';''')
        logger.info(SqlMdg)
        self.assertIn(constant.SYNTAX_ERROR_MSG, SqlMdg)

    #关键字作为目录对象名带反引号 - 合理报错
    def test_owner_4(self):
        SqlMdg = commonsh.execut_db_sql('''drop directory if exists \`owner\`;''')
        logger.info(SqlMdg)
        self.assertIn(constant.SYNTAX_ERROR_MSG, SqlMdg)

        SqlMdg = commonsh.execut_db_sql('''create directory \`owner\` as '/tmp/';''')
        logger.info(SqlMdg)
        self.assertIn(constant.SYNTAX_ERROR_MSG, SqlMdg)

    def tearDown(self):
        logger.info(f'-----{os.path.basename(__file__)} stop-----')
